<div class="layui-fluid layui-anim website-anim-up" id="website-review-add" lay-title="评审信息">
    <div class="layui-row layui-col-space8 website-container">
        <div class="layui-col-md10 layui-col-sm10 layui-col-xs10">
            <div class="layui-card" id="review-add">
                <div class="layui-card-header">基本情况</div>
                <div class="layui-card-body layui-form-item">
                    <div class="layui-row">
                        <!-- 基本信息-->
                        <div class="layui-inline layui-col-xs5 layui-col-sm5 layui-col-md5">
                            <div class="layui-col-xs4 layui-col-sm4 layui-col-md4">课题名称：</div>
                            <div class="layui-col-xs8 layui-col-sm8 layui-col-md8">[[${result.name}]]</div>
                        </div>
                        <div class="layui-inline layui-col-xs5 layui-col-sm5 layui-col-md5 layui-col-md-offset1 layui-col-lg-offset1">
                            <div class="layui-col-xs4 layui-col-sm4 layui-col-md4">指南序号：</div>
                            <div class="layui-col-xs8 layui-col-sm8 layui-col-md8">[[${result.topicCode}]]</div>
                        </div>
                    </div>
                    <div class="layui-row">
                        <div class="layui-inline layui-col-xs5 layui-col-sm5 layui-col-md5">
                            <div class="layui-col-xs4 layui-col-sm4 layui-col-md4">成果形式：</div>
                            <div id="achievementFormTypeCode" class="layui-col-xs8 layui-col-sm8 layui-col-md8"></div>
                        </div>
                        <div class="layui-inline layui-col-xs5 layui-col-sm5 layui-col-md5 layui-col-md-offset1 layui-col-lg-offset1">
                            <div class="layui-col-xs4 layui-col-sm4 layui-col-md4">成果字数：</div>
                            <div class="layui-col-xs8 layui-col-sm8 layui-col-md8">[[${result.achievementFormSize}]]
                            </div>
                        </div>
                    </div>
                    <div class="layui-row">
                        <div class="layui-inline layui-col-xs5 layui-col-sm5 layui-col-md5">
                            <div class="layui-col-xs4 layui-col-sm4 layui-col-md4">学科分类：</div>
                            <div id="subjectTypeCode" class="layui-col-xs8 layui-col-sm8 layui-col-md8"></div>
                        </div>
                        <div class="layui-inline layui-col-xs5 layui-col-sm5 layui-col-md5 layui-col-md-offset1 layui-col-lg-offset1">
                            <div class="layui-col-xs4 layui-col-sm4 layui-col-md4">研究类型：</div>
                            <div id="researchTypeCode" class="layui-col-xs8 layui-col-sm8 layui-col-md8"></div>
                        </div>
                    </div>
                    <div class="layui-row">
                        <div class="layui-inline layui-col-xs5 layui-col-sm5 layui-col-md5">
                            <div class="layui-col-xs4 layui-col-sm4 layui-col-md4">匿名附件：</div>
                            <div class="layui-col-xs8 layui-col-sm8 layui-col-md8">
                                <a id="upload-file-anonymous-a"></a>
                            </div>
                        </div>
                    </div>
                    <div class="layui-row">
                        <div class="layui-inline layui-col-xs5 layui-col-sm5 layui-col-md12">
                            <div class="layui-inline layui-col-xs5 layui-col-sm5 layui-col-md5 ">
                                <div class="layui-col-xs4 layui-col-sm4 layui-col-md4">课题研究情况：</div>
                                <textarea class="layui-textarea layui-disabled" name="researchContent" readonly
                                          autocomplete="off">[[${result.researchContent}]]</textarea>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <form class="layui-form" lay-filter="review-add-form" id="review-add-form">
                <div class="layui-card">
                    <div class="layui-card-header">评审信息</div>
                    <div class="layui-card-body layui-form-item">
                        <div id="expert-review-info"></div>
                        <div class="layui-row layui-form-item">
                            <label class="layui-form-label layui-form-label-two">评审意见：</label>
                            <div class="layui-inline layui-col-xs5 layui-col-sm5 layui-col-md12">
                                <textarea class="layui-textarea" id="researchContent" name="researchContent"
                                          placeholder="（内容提示：①与已有成果相比该成果的创新程度、突出特色和主要建树；②该成果的理论价值和应用价值；③该成果有何欠缺或不足，以及修改、提高的具体意见和建议；④成果转化建议。
                                          最多输入 1000 字）"
                                          autocomplete="off" maxlength="1000"></textarea>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="layui-form-item">
                    <button class="layui-btn" id="submit">提交</button>
                    <button class="layui-btn" id="save">保存</button>
                    <button class="layui-btn layui-btn-warm" id="clean">取消</button>
                </div>
            </form>
        </div>
    </div>
</div>

<script data-th-inline="javascript" type="text/javascript">
    layui.use(['jquery', 'laydate', 'form', 'website', 'dict', 'clientModules'], function () {
        let $ = layui.jquery,
            laydate = layui.laydate,
            website = layui.website,
            form = layui.form,
            client = layui.clientModules,
            view = layui.view,

            $form = $('#review-add-form'),
            $view = $('#website-review-add'),
            $submit = $('#submit'),
            $save = $('#save'),
            $clean = $('#clean'),

            result = [[${result}]],
            declareId = [[${id}]],
            reviewId = [[${reviewId}]],


            returnArrayData = [],
            returnData = {};

        initForm();


        function initForm() {
            if (result.anonymousFileId) {
                $("#upload-file-anonymous-a").text('点击下载');
                $("#upload-file-anonymous-a")[0].setAttribute('href', ctx + 'download/' + result.anonymousFileId)
            } else {
                $("#upload-file-anonymous-a").text('无附件');
            }
            website.get(ctx + 'review/getList/' + reviewId, null, function (data) {
                returnData = data.data;
                if (returnData.opinion) {
                    $('#researchContent').val(returnData.opinion)
                }
                returnArrayData = data.data.reviewSubjectList;
                let html = renderForm(data.data.reviewSubjectList)
                $('#expert-review-info').append(html)
                // 重新渲染 form否则不会显示下拉框
                form.render();
            })
        }

        // 提交
        $submit.on('click', function () {
            client.checkSubmit(sendAJAX, true)
            return false
        })


        // $save 0 为保存、1 为提交
        $save.on('click', function () {
            sendAJAX(false)
            return false
        })

        let sendAJAX = function (isSubmit) {
            let choseSize = 0;
            $.each(returnArrayData, function (i, item) {
                $.each(item.reviewDetailList, function (j, child) {
                    if (child.isChoose) {
                        ++choseSize;
                    }
                })
            })
            if (choseSize < returnArrayData.length) {
                website.alert.warn('您的评审结果填写还未完成！！');
                return false;
            }
            returnData.opinion = $('#researchContent').val()
            const json = JSON.stringify(returnData)
            website.ajaxPost(ctx + 'review/updateList', json, function (data) {
                if (isSubmit) {
                    website.ajaxPost(ctx + 'review/' + returnData.reviewId + '/commit', json, function (data) {
                        let currentUrl = window.location.hash.replace('#', '');
                        website.view.tab.del(currentUrl);
                        window.location.reload();
                        //  关闭当前页面
                        $('#website-home').find('#query').click();

                    })
                } else {
                    let currentUrl = window.location.hash.replace('#', '');
                    website.view.tab.del(currentUrl);
                    window.location.reload();
                    //  关闭当前页面
                    $('#website-home').find('#query').click();
                }
            });
        }


        $clean.on('click', function () {
            let currentUrl = window.location.hash.replace('#', '');
            website.view.tab.del(currentUrl);
            window.location.reload();
        })

        function renderForm(data) {
            let html = ''
            for (let i = 0; i < data.length; i++) {
                const parentNode = data[i]
                debugger
                html += '<ul>\n' +
                    '      <div class="website-form-item-require">' + (i + 1) + '、' + parentNode.indicatorName + ' - 权重：' + parentNode.weight + '</div>'

                for (let j = parentNode.reviewDetailList.length - 1; j >= 0; j--) {
                    let childNode = parentNode.reviewDetailList[j]
                    html += ' <li class="layui-row">\n' +
                        '          <div class="layui-col-md-offset1 layui-col-xs8 layui-col-sm8 layui-col-md8">'
                    html += childNode.indicatorType + '-' + childNode.questionDescription
                    html += '</div>\n' +
                        '         <div class="layui-col-md-offset1 layui-col-xs2 layui-col-sm2 layui-col-md2">\n' +
                        '           <select class="review-select" lay-filter="expert-option" reviewId="' + parentNode.reviewId + '" name="' + childNode.indicatorDetailId + '">'
                    html += ' <option  parentIndex="' + i + '" value="0">请选择</option>'
                    for (let k = childNode.minScore; k <= childNode.maxScore; k++) {
                        html += ' <option '
                        if (childNode.isChoose && childNode.score === k) {
                            html += 'selected '
                        }
                        html += 'childIndex="' + j + '"  parentIndex="' + i + '" value="' + k + '">' + k + '</option>'
                    }
                    html += ' </select>\n' +
                        '                                </div>\n' +
                        '                            </li>'
                }
                html += '</ul>'
            }
            return html;

        }


        form.on('select(expert-option)', function (data) {
            let id = data.elem.getAttribute('name'),
                selectedIndex = data.elem.selectedIndex,
                selectedOptions = data.elem.options[selectedIndex],
                childIndex = selectedOptions.getAttribute('childIndex'),
                parentIndex = selectedOptions.getAttribute('parentIndex'),
                parentNode = returnArrayData[parentIndex];
            if (!childIndex) {
                $.each(parentNode.reviewDetailList, function (i, item) {
                    if (item.isChoose) {
                        item.isChoose = false;
                    }
                })
                return
            }
            let childNode = parentNode.reviewDetailList[childIndex],
                value = data.value;

            // 1. 清除兄弟级别数组的 score 以及是否选中
            $(data.elem).parent().parent().parent().find('select').each(function (i, item) {
                if (item.getAttribute('name') !== id) {
                    $(this).val('');
                }
            })

            for (let i = 0; i < parentNode.reviewDetailList.length; i++) {
                let obj = parentNode.reviewDetailList[i]
                obj.score = 0
                obj.isChoose = false
            }

            // 2. 标记当前选择项分数以及是否选中
            childNode.score = value
            childNode.isChoose = true
            form.render();
        })

        // $("#topicCode").text(result.topicCode ? layui.dict.getByCode(result.topicCode).name : '');
        $("#achievementFormTypeCode").text(result.achievementFormTypeCode ? achievementFormTypeCodeName(result.achievementFormTypeCode) : '');
        $("#subjectTypeCode").text(result.subjectTypeCode ? layui.dict.getByCode(result.subjectTypeCode).name : '');
        $("#researchTypeCode").text(result.researchTypeCode ? layui.dict.getByCode(result.researchTypeCode).name : '');

        function achievementFormTypeCodeName(data) {
            let result = layui.dict.getByCodes(data.split(','))
            let name = ''
            for (let i = 0; i < result.length; i++) {
                name += result[i].name + ','
            }

            return name
        }

    })


</script>